﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace CaseReview.Utility
{
    public class Config {
        public static string Subject = "CaseReview";
        private static Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        public static string ConnStr {
            get {
                return ConfigurationManager.ConnectionStrings["dbLocalConnectionString"].ConnectionString;
            }
            set {
                _config.ConnectionStrings.ConnectionStrings["dbLocalConnectionString"].ConnectionString = value;
                _config.Save();
                ConfigurationManager.RefreshSection("connectionStrings");
            }
        }

        public static string Alias {
            get { return Environment.UserName; }
        }

        public static string EmailTemp {
            get { return "Email.html"; }
            set { SaveAppSetting("EmailTemp", value); }
        }
        public static string TeamAddress {
            get { return GetAppSetting("TeamAddress"); }
            set { SaveAppSetting("TeamAddress", value); }
        }
        public  static long GetDefaultSetting(string settinName) {
            long res = -1;
            Int64.TryParse(ConfigurationManager.AppSettings[settinName], out res);
            return res;
        }

        public static bool SaveDefaultSetting(string settingName,string value) {
            KeyValueConfigurationElement temp = new KeyValueConfigurationElement(settingName,value);
            _config.AppSettings.Settings.Remove(settingName);
            _config.AppSettings.Settings.Add(temp);
            _config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            return true;
        }

        public static string Cc {
            get { return GetAppSetting("Cc"); }
            set { SaveAppSetting("Cc", value); }
        }
        public static string GetAppSetting(string settingName) {
            return ConfigurationManager.AppSettings[settingName] ?? "";
        }

        public static bool SaveAppSetting(string settingName,string value) {
            KeyValueConfigurationElement temp = new KeyValueConfigurationElement(settingName,value);
            _config.AppSettings.Settings.Remove(settingName);
            _config.AppSettings.Settings.Add(temp);
            _config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            return true;
        }
    }
}
